package com.example.rbac.controller;

import com.example.rbac.annotation.RequirePermission;
import com.example.rbac.common.Result;
import com.example.rbac.model.User;
import com.example.rbac.service.UserService;
import com.example.rbac.utils.UserContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
@Api(tags = "用户管理")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @GetMapping("/list")
    @ApiOperation("获取用户列表")
    @RequirePermission(value = "user:list", name = "查看用户列表")
    public Result<?> getUserList(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               String username) {
        Page<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (username != null && !username.isEmpty()) {
            queryWrapper.like("username", username);
        }
        Page<User> userPage = userService.page(page, queryWrapper);
        return Result.success(userPage);
    }
    
    @GetMapping("/{id}")
    @ApiOperation("获取用户详情")
    @RequirePermission(value = "user:info", name = "查看用户详情")
    public Result<?> getUserById(@PathVariable Long id) {
        User user = userService.getById(id);
        if (user == null) {
            return Result.error("用户不存在");
        }
        return Result.success(user);
    }
    
    @PostMapping
    @ApiOperation("新增用户")
    @RequirePermission(value = "user:add", name = "新增用户")
    public Result<?> addUser(@RequestBody User user) {
        userService.save(user);
        return Result.success("添加成功");
    }
    
    @PutMapping
    @ApiOperation("修改用户")
    @RequirePermission(value = "user:edit", name = "修改用户")
    public Result<?> updateUser(@RequestBody User user) {
        userService.updateById(user);
        return Result.success("修改成功");
    }
    
    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    @RequirePermission(value = "user:delete", name = "删除用户")
    public Result<?> deleteUser(@PathVariable Long id) {
        userService.removeById(id);
        return Result.success("删除成功");
    }
    
    /**
     * 获取当前登录用户信息（演示ThreadLocal的使用）
     */
    @GetMapping("/current")
    @ApiOperation("获取当前登录用户信息")
    @RequirePermission(value = "user:info", name = "查看当前用户信息")
    public Result<?> getCurrentUser() {
        // 使用ThreadLocal获取当前登录用户信息
        Long userId = UserContext.getUserId();
        String username = UserContext.getUsername();
        
        // 构建返回结果
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("userId", userId);
        userInfo.put("username", username);
        
        // 可以根据需要获取更多用户信息
        User user = userService.getById(userId);
        if (user != null) {
            userInfo.put("nickname", user.getNickname());
            userInfo.put("email", user.getEmail());
            userInfo.put("phone", user.getPhone());
            userInfo.put("avatar", user.getAvatar());
            userInfo.put("status", user.getStatus());
        }
        
        return Result.success(userInfo);
    }
}